<?php

/**
 * 获取数组val值中的字段
 * @param Array $arr
 * @param string $field
 * return Array
 */
	function wx2uid($wxid,$wx){
		if($wx == 1){
			$key = 'weixinid';
		}elseif($wx == 2){
			$key = 'weixinid2';
		}
		$user = D('wx2_user')->where($key.' = "'.$wxid.'"')->find();
		if(empty($user)){
			return 0;
		}else{
			return $user['id'];
		}
	}
	
	function vpost($url,$data){ // 模拟提交数据函数
		$curl = curl_init(); // 启动一个CURL会话
		curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
		curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查
		curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1); // 从证书中检查SSL加密算法是否存在
		curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器
		curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
		curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
		curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
		curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // Post提交的数据包
		curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
		curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
		$tmpInfo = curl_exec($curl); // 执行操作
		if (curl_errno($curl)) {
			echo 'Errno'.curl_error($curl);//捕抓异常
		}
		curl_close($curl); // 关闭CURL会话
		return $tmpInfo; // 返回数据
	}
	
	function getcompanybycid($cid){
		$company = D('company_info')->where('cid = '.$cid)->find();
		return $company;
	}
	//职位类型
	function getjobtype($type){
		$jobtype = D('position')->where('pid = '.$type)->find();
		return $jobtype;
	}
	
	function getworktime($time){
		$jobtime = D('work_time')->where('wid = '.$time)->find();
		return $jobtime['time'];
	}
	
	function getxueli($xueli){
		$xueli = D('education')->where('eid = '.$xueli)->find();
		return $xueli['edu'];
	}
	function getposition($hid){
		$position = D('position')->where('hid = '.$hid)->findAll();
		return $position;
	}
	
	function getworkstore($store){
		if($store == 0){
			return '就近分配';
		}else{
			$storearray = D('store')->where('store_id IN ('.$store.')')->findAll();
			foreach($storearray as $k => $v){
				$storeval[] = $v['store_name'];
			}
			return implode(' ',$storeval);
		}
	}
	
	function getexperience($experience){
		$experience = D('experience')->where('life_id = '.$experience)->find();
		return $experience['life_name'];
	}
	
	function getstoreaddress($pro,$city,$area,$address){
		$areaarray = D('area')->findAll();
		foreach($areaarray as $k => $v){
			$areas[$v['area_id']] = $v['title'];
		}
		return $areas[$pro].$areas[$city].$areas[$area].$address;
	}
	
	function getstoresid($store){
		$storearray = D('store')->where('store_id IN ('.$store.')')->findAll();
		foreach($storearray as $k => $v){
			$area[$v['store_id']] = array('pro'=>$v['pro'],'city'=>$v['city'],'area'=>$v['area'],'address'=>$v['address']);
		}
		return $area;
	}
	
	function getmssj($mssj){
		$week = array(
					1=>'周一',
					2=>'周二',
					3=>'周三',
					4=>'周四',
					5=>'周五',
					6=>'周六',
					7=>'周日'
		);
		$mssjarray = explode(',',$mssj);
		foreach($mssjarray as $k => $v){
			$array[] = $week[$v];
		}
		return implode(' ',$array);
	}
	
	function checkphone($phone){
		$result = D('user')->where('login = "'.$phone.'"')->find();
		if(!empty($result)){
			return false;
		}else{
			return true;
		}
	}
	
	


//简历格式化
function jlformat($r){
	if(!empty($r['xingbie'])){
		switch($r['xingbie']){
			case 1:$r['xingbie'] = '帅哥';break;
			case 2:$r['xingbie'] = '美女';break;
		}
	}
	if(!empty($r['shengri'])){
		$large = time()-strtotime($r['shengri']);
		$r['age'] = floor($large/31536000);
	}
	if(!empty($r['hunyin'])){
		switch($r['hunyin']){
			case 1:$r['hunyin'] = '未婚';break;
			case 2:$r['hunyin'] = '已婚未育';break;
			case 3:$r['hunyin'] = '已婚已育';break;
		}
	}
	if(!empty($r['xueli'])){
		$r['xueli'] =  D('education')->where('eid = '.$r['xueli'])->getField('edu');;
	}
	/*if(!empty($r['xueli'])){
		switch($r['xueli']){
			case 1:$r['xueli'] = '保密';break;
			case 2:$r['xueli'] = '小学';break;
			case 3:$r['xueli'] = '初中';break;
			case 4:$r['xueli'] = '高中';break;
			case 5:$r['xueli'] = '中专/中职';break;
			case 6:$r['xueli'] = '大专';break;
			case 7:$r['xueli'] = '本科及以上';break;
		}
	}
	if(!empty($r['eduxueli'])){
		switch($r['eduxueli']){
			case 1:$r['eduxueli'] = '保密';break;
			case 2:$r['eduxueli'] = '小学';break;
			case 3:$r['eduxueli'] = '初中';break;
			case 4:$r['eduxueli'] = '高中';break;
			case 5:$r['eduxueli'] = '中专/中职';break;
			case 6:$r['eduxueli'] = '大专';break;
			case 7:$r['eduxueli'] = '本科及以上';break;
		}
	}*/
	if(!empty($r['yhyf'])){
		$hangye = D('hangye')->where('id IN('.$r['yhyf'].')')->findAll();
		foreach($hangye as $k => $v){
			$yhyf[] = $v['h_name'];
		}
		$r['yhyf'] = implode(',',$yhyf);
	}
	if(!empty($r['zhuangtai'])){
		switch($r['zhuangtai']){
			case 1:$r['zhuangtai'] = '正在找工作';break;
			case 2:$r['zhuangtai'] = '有好的机会可以考虑';break;
			case 3:$r['zhuangtai'] = '暂时不找工作';break;
		}
	}
	return $r;
}



function formatrdate($data){
	return date('Y年m月',strtotime($data));
}

function getpicbyfid($fid){
	$file = D('file')->where('id = '.$fid)->find();
	if($file['remote']==0){
		return 'http://www.yingyeyuan.com/data/files'.$file['base_url'];
	}else{
		return site_url.$file['base_url'];
	}
}
function gethangye(){
	return D('hangye')->findAll();
}
function getedu(){
	$return = D('education')->select();
	return $return;
}
function getjobspics(){
	$files = D('jobs_pic')->where('uid = '.$_SESSION['wxuser']['uid'].' AND is_del=0')->findAll();
	foreach($files as $k => $v){
		$fileinfo = D('file')->where('id = '.$v['fid'])->find();
		$imgs[] = array(
					'imgid' => $v['fid'],
					'url' => site_url.$fileinfo['base_url']
				);
	}
	return $imgs;
}
function getjobstype(){
	$jobstype = D('jobs_type')->findAll();
	return $jobstype;
}
function getwork_time(){
	$worktime = D('work_time')->findAll();
	return $worktime;
}

/*
 * 动态获取地区信息
 */
function getarea($type = 'pro',$aid = '0'){
	if($type == 'pro'){
		$return = D('area')->where('pid = 0')->select();
	}
	if($type == 'city'){
		$return = D('area')->where('pid = '.$aid)->select();
	}
	if($type == 'area'){
		$return = D('area')->where('pid = '.$aid)->select();
	}
	return $return;
}


/*
 * 根据area id 获取名称
 */
function getareaname($aid){
	$return = D('area')->where('area_id = '.$aid)->find();
	return $return['title'];
}

/*
 * 根据企业 id 获取企业名称
 */
function getcompany($cid){
	$return = D('company')->where('cid = '.$cid)->find();
	return $return;
}

function format_brand($brand){
	$brandarray = explode(',',$brand);
	foreach($brandarray as $k => $v){
		$brands[$k] = D('brand')->where('bid = '.$v)->find();
	}
	return $brands;
}
function format_xingbie($xingbie){
	switch($xingbie){
		case 0:$xingbie = '不限';break;
		case 1:$xingbie = '帅哥';break;
		case 2:$xingbie = '美女';break;
	}
	return $xingbie;
}

function format_age($shengri){
	$large = time()-strtotime($shengri);
	$age = floor($large/31536000);
	return $age;
}
function format_worktime($wid){
	$worktime = D('work_time')->where('wid = '.$wid)->getField('time');
	return $worktime;
}
function format_xueli($eid){
	$xueli = D('education')->where('eid = '.$eid)->getField('edu');
	return $xueli;
}
function format_companyname($cid){
	$return = D('company')->where('cid = '.$cid)->getField('company_name');
	return $return;
}
function format_exper($exper){
	$expers = C('EXPER_CONFIG');
	return $expers[$exper];
}
function format_height($height){
	$heights = C('HEIGHT_CONFIG');
	return $heights[$height];
}
function format_workplace($stores){
	$key = 0;
	$area[] = $stores[0]['area'];
	foreach($stores as $k => $v){
		if(!in_array($v['area'],$area)){
			$area[count($area)] = $v['area'];
		}
	}
	foreach($area as $k => $v){
		foreach($stores as $a => $b){
			if($b['area']==$v){
				$store[$b['area']]['stores'][count($store[$b['area']]['stores'])]=$b;
			}
		}
	}
	foreach($store as $k => &$v){
		$v['area'] = getareaname($k);
	}
	return $store;
}
function format_workplace_page($stores){
	$key = 0;
	$area[] = $stores['data'][0]['area'];
	foreach($stores['data'] as $k => $v){
		if(!in_array($v['area'],$area)){
			$area[count($area)] = $v['area'];
		}
	}
	foreach($area as $k => $v){
		foreach($stores['data'] as $a => $b){
			if($b['area']==$v){
				$store[$b['area']]['stores'][count($store[$b['area']]['stores'])]=$b;
			}
		}
	}
	foreach($store as $k => &$v){
		$v['area'] = getareaname($k);
	}
	return $store;
}

/*上传音频文件到又拍云*/
function uploadvoice($type,$file){
	import('ORG.Util.upyun');
	$upload = new delayupload();
	$result = $upload->upload_voice($type,$file);
	$vid = D('attachment')->add($result);
	return $vid;
}

/*上传头像文件到又拍云*/
function uploadface($type,$file){
	import('ORG.Util.upyun');
	$upload = new delayupload();
	$result = $upload->upload_face($type,$file);
	$fid = D('attachment')->add($result);
	return $fid;
}

function GetDistance($lat1, $lng1, $lat2, $lng2, $len_type = 1, $decimal = 2) 
{ 
	define('EARTH_RADIUS', 6378.137);//地球半径 
	define('PI', 3.1415926);
	$radLat1 = $lat1 * PI / 180.0; 
	$radLat2 = $lat2 * PI / 180.0; 
	$a = $radLat1 - $radLat2; 
	$b = ($lng1 * PI / 180.0) - ($lng2 * PI / 180.0); 
	$s = 2 * asin(sqrt(pow(sin($a/2),2) + cos($radLat1) * cos($radLat2) * pow(sin($b/2),2))); 
	$s = $s * EARTH_RADIUS; 
	$s = round($s * 1000); 
	if ($len_type > 1){ 
		$s /= 1000; 
	} 
	return round($s, $decimal); 
} 
?>
		